<?php 
global $_GPC, $_W;
$cfg = $this->module['config'];
$shopid=check_right(0);
if(!$shopid)message("请先选择需要管理的店铺",$this->createWebUrl('shop'),'error');
$shop=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid = '{$_W['uniacid']}' and id='".$shopid."'");
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
load()->func('tpl');

if(!pdo_fieldexists('j_hmoney_goodpackage', 'price')) {
    pdo_query("ALTER TABLE ".tablename('j_hmoney_goodpackage')." ADD `price` decimal(10,2) NOT NULL COMMENT '零售价格' ;");
}


if ($operation == 'display') {
	$condition = " and groupid ='".$shopid."' ";
	if (isset($_GPC['keyword']))$condition .= " and (title like '%" . $_GPC['keyword'] . "%')";
	if ($_GPC['status1'])$condition .= " and status = '" . ($_GPC['status1']-1) . "' ";
	$list = pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_goodpackage')." WHERE weid = '{$_W['uniacid']}' $condition order by displayorder asc, id desc  ");


} elseif ($operation == 'changecateorder') {
    foreach($_GPC['displayorder'] as $index=>$row){
        pdo_update('j_hmoney_goodpackage',array("displayorder"=>$row),array("id"=>$index));
    }
    die(json_encode(array("success"=>true)));

} elseif ($operation == 'getgoods') {
	$cid = intval($_GPC['cid']);
    $keyword = trim($_GPC['keyword']);
	if(empty($cid) && empty($keyword))die(json_encode(array("success"=>false,"msg"=>"请选择商品")));
    $condition="";
    if($keyword){
        $condition.=" and (title like '%".$keyword."%' or goodsn like '%".$keyword."%')";
        $list=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_goods')." WHERE groupid=:a $condition order by displayorder asc ,id desc",array(':a'=>$shopid));
    }else{
        $list=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_goods')." WHERE pcate = :a order by displayorder asc ,id desc",array(':a'=>$cid));
    }

    for($i=0;$i<count($list);$i++){
        if($list[$i]["thumb"]){
            $temp=$list[$i]["thumb"];
            $list[$i]["thumb"]=tomedia($temp);
        }
    }
	die(json_encode(array("success"=>true,"list"=>$list)));

} elseif ($operation == 'getonegood') {
    $id = intval($_GPC['id']);
    if(empty($id))die(json_encode(array("success"=>false,"msg"=>"请选择商品")));
    $item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_goods')." WHERE id = :a ",array(':a'=>$id));
    if($item && $item["thumb"]){
        $temp=$item["thumb"];
        $item["thumb"]=tomedia($temp);
    }
    die(json_encode(array("success"=>true,"item"=>$item)));
	
} elseif ($operation == 'post') {

    $id=$_GPC['id'];
    if($id){
        $item = pdo_fetch("SELECT * FROM ".tablename('j_hmoney_goodpackage')." WHERE  id=:a ",array(":a"=>$id));
        if($item['goodids']){
            $goodtemp=explode(",",$item['goodids']);
            $gids=array();
            $goodoldary=array();
            foreach($goodtemp as $row){
                $temp=explode(":",$row);
                $gids[]=$temp[0];
                $goodoldary[]=$temp[1];
            }
            $goodlist = pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_goods')." WHERE weid = '{$_W['uniacid']}' and id in (".implode(",",$gids).") order by id desc  ");
            $goodary=array();
            foreach($goodlist as $row){
                $goodary[$row['id']]=$row;
            }
        }
    }
    $goodpcate = pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_goodcategory')." WHERE weid = '{$_W['uniacid']}' and groupid=:a order by displayorder asc, id desc  ",array(":a"=>$shopid));
    if (checksubmit('submit')) {
        $data = array(
            'weid' => $_W['uniacid'],
            'title' => $_GPC['title'],
            'groupid' => intval($shopid),
            'thumb' => $_GPC['thumb'],
            'price' => $this->_2money(($_GPC['price']) * 100),
            'memberprice' => $this->_2money(($_GPC['memberprice']) * 100),
            'goodids' => $_GPC['goods'],
            'status' => intval($_GPC['status']),
            'createtime' => TIMESTAMP,
        );
        if (!empty($id)) {
            unset($data['createtime']);
            pdo_update('j_hmoney_goodpackage', $data, array('id' => $id));
        } else {
            pdo_insert('j_hmoney_goodpackage', $data);
        }
        message("修改成功",$this->createWebUrl('goodspackage'));
    }
} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $item = pdo_fetch("SELECT id FROM " . tablename('j_hmoney_goodpackage') . " WHERE id = '$id'");
    if($item)pdo_delete('j_hmoney_goodpackage', array('id' => $id));
    message('删除成功！', $this -> createWebUrl('goodspackage'), 'success');
}
include $this->template('web/goodspackage');